package com.changgou.pay.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class RabbitMQConfiguration {

    //用于加载配置文件数据
    @Autowired
    private Environment environment;

    //创建交换机
    @Bean
    public Exchange exchange() {
        return new DirectExchange(environment.getProperty("mq.pay.exchange.order"), true, false);
    }

    //创建队列(普通支付)
    @Bean
    public Queue queue() {
        return new Queue(environment.getProperty("mq.pay.queue.order"), true, false, false);
    }

    //绑定队列与交换机(普通支付)
    @Bean
    public Binding binding(Exchange exchange, Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(environment.getProperty("mq.pay.routing.key")).noargs();
    }

    //创建队列(秒杀支付)
    @Bean
    public Queue seckillQueue() {
        return new Queue(environment.getProperty("mq.pay.queue.seckillorder"), true, false, false);
    }

    //绑定队列与交换机(秒杀支付)
    @Bean
    public Binding seckillBinding(Exchange exchange, Queue seckillQueue) {
        return BindingBuilder.bind(seckillQueue).to(exchange).with(environment.getProperty("mq.pay.routing.seckillorderkey")).noargs();
    }
}
